<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop" ref="buttondiv">
          <table style="width:100%">
              <tr>
                  <td>
                      <table>
                          <tr>
                              <td width="50" style="text-align:right">生成日期</td>
                              <td width="280">
                                  <el-date-picker v-model="datetime1" style="width:46%" type="date" placeholder="开始日期" value-format="yyyy-MM-dd 00:00:00" :clearable="false"></el-date-picker> 至
                                  <el-date-picker v-model="datetime2" style="width:46%" type="date" placeholder="结束日期" value-format="yyyy-MM-dd 23:59:59" :clearable="false"></el-date-picker>
                              </td>
                              <td style="text-align:right">回单条码</td>
                              <td>
                                  <el-input v-model="collectbarcode"></el-input>
                              </td>
                              <td width="40" style="text-align:right">病历号</td>
                              <td width="120">
                                  <el-input v-model="patno"></el-input>
                              </td>
                              <td width="30" style="text-align:right">状态</td>
                              <td>
                                  <rm-ddl-common datatype="seccode" kindno="REQ_STAT" dataname="状态" :appendtobody="true" v-model="reqstat" width="160px"></rm-ddl-common>
                              </td>
                              <td rowspan="2">
                                  <el-button style="width:100%;height:56px" @click.native="BtnReadCard">读卡</el-button>
                              </td>
                          </tr>
                          <tr>
                              <td style="text-align:right">条形码</td>
                              <td width="120">
                                  <el-input v-model="barcode"></el-input>
                              </td>
                              <td style="text-align:right">病人类型</td>
                              <td>
                                  <rm-ddl-common datatype="seccode" kindno="PT" dataname="病人类型" :appendtobody="true" v-model="pattype" width="160px"></rm-ddl-common>
                              </td>
                              <td style="text-align:right">姓名</td>
                              <td width="120">
                                  <el-input v-model="patname"></el-input>
                              </td>

                              <td width="30" style="text-align:right">卡号</td>
                              <td>
                                  <el-input v-model="cardno" width="160px"></el-input>
                              </td>
                          </tr>
                      </table>
                  </td>
              </tr>
              <tr>
                  <td>
                      <table>
                          <tr>
                              <td width="2000px"></td>
                              <td>
                                  <el-button icon="el-icon-search" type="primary" @click="FindData(0)" v-has="'PINQUERY'">查询</el-button>
                              </td>
                              <td>
                                  <el-button icon="el-icon-delete" @click="Clear()">清除</el-button>
                              </td>
                              <td>
                                  <el-button icon="el-icon-tickets" @click="Submit()">取消采样</el-button>
                              </td>
                          </tr>
                      </table>
                  </td>
              </tr>
          </table>
      </div>
      <div class="pagecenter">
          <rm-layout-right splitcolor="#F8F8FF" movingcolor="#ccc" :splitwidth="3" :fixsize="300" :minfixsize="300">
              <div style="height:100%">
                  <vxe-table :data="tableData" :loading="loading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :select-config="selectConfig" :sort-config="{trigger: 'cell'}" :edit-config="{trigger: 'click', mode: 'cell'}" @current-change="currentChangeEvent" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="xTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                      <vxe-table-column type="selection" width="40" header-align="left" align="center"></vxe-table-column>
                      <vxe-table-column :key="item.field" :field="item.field" :title="item.title" :width="item.width" v-for="item in tableColumn" sortable :align="item.field == 'emer_flag'?'center':'left'" header-align="center">
                          <template v-if="item.field == 'emer_flag'" v-slot="{ row }">
                              <div v-if="row.emer_flag == '1'" style="color: red">急</div>
                              <div v-else></div>
                          </template>
                      </vxe-table-column>
                  </vxe-table>
              </div>
              <template v-slot:fix>
                  <div ref="xTableDiv1" style="height:100%">
                      <vxe-table :data.sync="tableDataDetail" :select-config="selectConfig" height="auto" :sort-config="{trigger: 'cell'}" :loading="loadingDetail" highlight-current-row highlight-hover-row border fit highlight-hover-column resizable header-align="center" show-header-overflow="tooltip" show-overflow="tooltip" ref="xTable2" size="mini" stripe>
                          <vxe-table-column :key="item.field" :field="item.field" :title="item.title" :width="item.width" v-for="item in tableColumnDetail" header-align="center"></vxe-table-column>
                      </vxe-table>
                  </div>
              </template>
          </rm-layout-right>
      </div>

      <el-dialog title="已接收条码退回信息录入" :visible.sync="dialogBackinfo" v-el-drag-dialog :close-on-click-modal="false" width="80%">
          <div style="width:100%;height:560px">
              <rm-layout-top splitcolor="white" :splitwidth=5 :fixsize=40>
                  <template v-slot:fix>
                      <div style="height:40px;border:1px solid #CCC">
                          <table>
                              <td><span style="font-size:800;color:black;font-weight:bold">已录入条码退回处理：</span></td>
                              <td>
                                  <el-select v-model="rejhandflag">
                                      <el-option v-for="(item,index) in rejhandlistOption" :label="item.label" :key="index" :value="item.value">
                                          <span style="float: left">{{ item.label }}</span>
                                          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                      </el-option>
                                  </el-select>
                              </td>
                              <td>
                                  <el-button type="primary" @click="Backinfosave">退 回</el-button>
                                  <el-button @click="dialogBackinfo = false">取 消</el-button>
                              </td>
                          </table>
                      </div>
                  </template>

                  <div style="height:100%;width:100%;">
                      <vxe-table show-overflow ref="xTableBack" height="auto" row-id="id" :select-config="{reserve: true,checkMethod: checkMethod,}" :data.sync="recstatusdata" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe :edit-config="{trigger: 'click',mode: 'cell',showStatus: true,showIcon: false,}">
                          <vxe-table-column type="selection" width="40" :edit-render="{ name: 'selection' }" header-align="center" align="center"></vxe-table-column>
                          <vxe-table-column field="reject_reason" title="退回原因" width="102" header-align="center" align="center" :edit-render="{ name: 'select', options: RejectiReasonOption }">
                              <template v-slot:header="{ column }">
                                  <span style="color: blue">退回原因</span>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column field="description" title="备注 " width="167" header-align="center" :edit-render="{ name: 'input', autoselect: true }">
                              <template v-slot:header="{ column }">
                                  <span style="color: blue">备注</span>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column field="tellman" title="临床通知人 " width="117" header-align="center" :edit-render="{ name: 'input', autoselect: true }" v-show="rejectreqmusttell == '1'">
                              <template v-slot:header="{ column }">
                                  <span style="color: blue">临床通知人</span>
                              </template>
                          </vxe-table-column>
                          <!--<vxe-table-column field="photoimage" title="拍照" width="40px" align="center" v-if="isshow">
                      <template v-slot="{ row }">
                          <i class="el-icon-camera" style="padding: 5px 10px !important" @click="cameraEvent(row)"></i>
                      </template>
                  </vxe-table-column>-->
                          <vxe-table-column field="req_groupna" title="分单类别" width="100" header-align="center"></vxe-table-column>
                          <vxe-table-column field="req_deptname" title="申请科室" width="84" header-align="center"></vxe-table-column>
                          <vxe-table-column field="req_wardname" title="病人病区" width="85" header-align="center"></vxe-table-column>
                          <vxe-table-column field="telnum" title="病区电话" width="90" header-align="center"></vxe-table-column>
                          <!--病区电话待改视图后修改，暂定取值wardtelnum,具体名称为视图修改后的名称-->
                          <vxe-table-column field="barcode" title="条码号" width="100" header-align="center" align="center"></vxe-table-column>
                          <vxe-table-column field="req_bedno" title="病人床号" width="74" header-align="center" align="center"></vxe-table-column>
                          <vxe-table-column field="pat_name" title="病人姓名" width="74" header-align="center"></vxe-table-column>
                          <vxe-table-column field="pat_no" title="病人号 " width="105" header-align="center"></vxe-table-column>
                          <vxe-table-column field="pat_sexname" title="性别" width="48" header-align="center" align="center"></vxe-table-column>
                          <vxe-table-column field="req_statname" title="申请状态" width="72" header-align="center"></vxe-table-column>
                          <vxe-table-column field="req_groupid" title="分单类别ID" width="100" header-align="center"></vxe-table-column>
                          <vxe-table-column field="barcode" title="条码号" width="127" header-align="center"></vxe-table-column>
                          <vxe-table-column field="emer_flag" title="加急" width="60" header-align="center">
                              <template v-slot="{ row }">
                                  <div style="color: red">
                                      {{ row.emer_flag == "1" ? "急" : "" }}
                                  </div>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column field="sample_items" title="项目描述" width="203" header-align="center"></vxe-table-column>
                          <vxe-table-column field="base_amount" title="基准总金额" width="85" header-align="center" align="right"></vxe-table-column>
                          <vxe-table-column field="req_docname" title="申请医生" width="95" header-align="center"></vxe-table-column>
                          <vxe-table-column field="pat_typename" title="病人类型" width="97" header-align="center"></vxe-table-column>
                          <vxe-table-column field="create_dt" title="条码生成时间" width="130" header-align="center" :formatter="['toDateString', 'yyyy-MM-dd']"></vxe-table-column>
                          <vxe-table-column field="create_username" title="条码生成用户" width="104" header-align="center"></vxe-table-column>
                          <vxe-table-column field="rejectmsg" title="退回信息 " width="165" header-align="center"></vxe-table-column>
                      </vxe-table>
                  </div>

              </rm-layout-top>

          </div>
          <div slot="footer" class="dialog-footer">

          </div>
      </el-dialog>
  </div>
</template>

<script>
// import { parse } from "path";
export default {
  name: "REQ115",
  //以下是还未完成的功能
  //配合选项的三个申请单项目状态判断没做
  //删除his_execute中记录的his_recordid的记录，参考门诊采样当时生成的逻辑，涉及医院代码、病人类型、his_recordid联合条件
  //执行申请单计划：if gn_plan.uf_execplan_req(adc_reqid,'9',as_user,as_errmsg) < 0 then
  //同步HIS接口：IF gn_plan.uf_hisplan_add("REQSYNC_UNCREATE",adc_reqid,ls_barcode,adt_time,as_user,'条码作废同步') <> 0 THEN
  //退费HIS接口：IF gn_inter.uf_chargereq(adc_reqid,as_performby,true,as_user,adt_time,as_errmsg,ls_ret2) < 0 THEN
  data() {
      return {
          datetime1: "",
          datetime2: "",
          patname: "",
          patno: "",
          barcode: "",
          collectbarcode: "",
          pattype: "1",
          cardno: "",
          reqstat: "",
          RejectiReasonOption: [],
          rejectreqmusttell: "",
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
          },
          keyLocalConfig: {
              isArrow: true,
          },
          selectConfig: { tigger: "row" },
          loading: false,
          loadingDetail: false,
          tableData: [],
          tableDataAll: [],
          tableDataDetail: [],
          tableColumn: [],
          tableColumnDetail: [],
          interfacename: "",
          CancelVisible: false,
          reqmaster: {},
          reqitem: "",
          // patno: "",
          UpPatno: "",
          noArray: [],
          cancelsampletipsback: false,
          dialogBackinfo: false,
          recstatusdata: [],
          loginuserno: this.$tool.getSession(this.$tool.loginuserno),
          rejhandlistOption: [
              { label: "退回", value: "0" },
              { label: "删除未审核", value: "1" },
              { label: "修改条码号", value: "2" },
          ],
          rejhandflag: "",
      };
  },
  created() {
      this.InitPageData();
      this.ddlInitData();
  },
  mounted() {},
  updated() {},
  methods: {
      currentChangeEvent({ row }) {
          this.reqmaster = row;
          this.FindDetail();
      },
      InitPageData() {
          var day1 = new Date();
          day1.setTime(day1.getTime());
          var s1 = day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate() + " 23:59:59";
          var day2 = new Date();
          day2.setTime(day2.getTime() - 24 * 60 * 60 * 1000 * 7);
          var s2 = day2.getFullYear() + "-" + (day2.getMonth() + 1) + "-" + day2.getDate() + " 00:00:00";
          this.datetime1 = s2;
          this.datetime2 = s1;
          this.FindData(1);
          //this.FindDetail();
      },
      FindData(qtype) {
          this.loading = true;
          this.$tool
              .request({
                  url: "REQ/REQ1/REQ115",
                  method: "post",
                  data: {
                      action: "query",
                      barcode: this.barcode,
                      begindate: this.datetime1,
                      enddate: this.datetime2,
                      patno: this.patno,
                      patname: this.patname,
                      collectbarcode: this.collectbarcode,
                      pattype: this.pattype,
                      reqstat: this.reqstat,
                      cardno: this.cardno,
                      qtype: qtype,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      this.tableColumn = res.Cols;
                      this.tableData = res.Data1;
                      if (this.tableData.length > 0) {
                          this.$refs.xTable.setCurrentRow(res.Data1[0]);
                          this.reqmaster = res.Data1[0];
                      }
                      this.FindDetail();
                  } else {
                      this.$alert(res.errmsg, "提示");
                  }
                  this.loading = false;
              });
      },
      FindDetail() {
          this.loadingDetail = true;
          this.$tool
              .request({
                  url: "REQ/REQ1/REQ115",
                  method: "post",
                  data: {
                      action: "querydetail",
                      barcode: this.reqmaster.barcode,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      this.tableColumnDetail = res.Data.Cols;
                      this.tableDataDetail = res.Data.Data;
                  }
                  this.loadingDetail = false;
              });
      },
      Clear() {
          this.tableData = [];
          this.tableDataDetail = [];
          this.barcode = "";
          this.datetime1 = "";
          this.datetime2 = "";
          this.patname = "";
          this.patno = "";
          this.cardno = "";
          this.collectbarcode = "";
          this.reqstat = "";
      },
      ddlInitData() {
          this.$tool
              .request({
                  url: "REQ/REQ1/REQ115",
                  method: "post",
                  data: {
                      action: "InitData",
                  },
              })
              .then((res) => {
                  if (res && !res.hasOwnProperty("errcode")) {
                      this.interfacename = res.opt; //调用读卡exe
                      this.cancelsampletipsback = res.cancelsampletipsback == "1";
                      this.RejectiReasonOption = res.RejectiReasonOption;
                      this.rejectreqmusttell = res.rejectreqmusttell;
                  }
              })
              .catch((e) => {
                  console.log("error", e);
              });
      },
      BtnReadCard() {
          this.$pub
              .readcard(this, this.interfacename)
              .then((res) => {
                  this.patno = res;
                  if (this.patno == "") {
                      return false;
                  }
                  this.FindData(0);
              })
              .catch((e) => {});
      },
      Submit() {
          var record = this.$refs.xTable.getSelectRecords();
          // for (let i = 0, len = record.length; i < len; i++) {
          //     this.$tool
          //         .request({
          //             url: "REQ/REQ1/REQ115",
          //             method: "post",
          //             data: {
          //                 action: "cancelall",
          //                 barcode: record[i].barcode,
          //             },
          //         })
          //         .then((res) => {
          //             if (res.flag == 1) {
          //                 record[i].req_stat = "9";
          //                 record[i].req_statname = "作废/取消";
          //             } else {
          //                 this.$message.error("取消采样失败:" + res.msg);
          //             }
          //         });
          // }
          if (this.cancelsampletipsback) {
              //勾选选项取消门诊采样时，对已经接收的条码，弹窗提示是否先进行退回
              this.recstatusdata = record.filter((m) => m.req_stat >= 4 && m.req_stat != 6 && m.req_stat != 8 && m.req_stat != 9);
              //申请状态为接收的数据,6确认报告，8标本剔回，9已作废的勾选不显示在待退回界面
              if (this.recstatusdata.length > 0) {
                  this.dialogBackinfo = true;
                  this.scanbarcode(this.recstatusdata); //将待退回条码扫描一遍获取rejectmsg，退回时参数
                  this.$alert("所选数据包含已接收条码,已接收条码退回时需填写退回信息！", "提示");
              }

              let otherstatusdata = record.filter((m) => m.req_stat < 4); //申请状态为非接收的数据，直接取消采样
              this.cancelall(otherstatusdata);
          } else {
              this.cancelall(record);
          }
      },
      scanbarcode(data) {
          for (let index = 0; index < data.length; index++) {
              const element = data[index];
              this.$tool
                  .request({
                      url: "REQ/REQ1/REQ115",
                      method: "post",
                      data: {
                          action: "scanbarcode",
                          barcode: element.barcode,
                      },
                  })
                  .then((res) => {
                      if (res.flag == "-1") {
                          //-1禁止退回
                          this.$alert(res.msg, "提示");
                          //element.msginfo = res.rejectmsg;
                          element.rejectmsg = res.rejectmsg;
                          element.chkflag = "0"; //是否可勾选--不可勾选
                          this.$refs.xTableBack.setSelection(element, false);
                      } else {
                          //-99 根据选项退回
                          element.rejectmsg = res.rejectmsg;
                          element.chkflag = "1"; //是否可勾选--不可勾选
                          this.$refs.xTableBack.setSelection(element, true);
                      }
                  })
                  .catch((e) => {
                      this.$alert(e, "提示");
                  });
          }
      },
      //退回保存
      Backinfosave() {
          if (this.rejhandflag == "") {
              this.$alert("请选择已录入条码处理方式。", "提示");
          } else {
              let rejectinfo = [];
              let err = "";
              let tabledata = this.$refs.xTableBack.getSelectRecords(); //tableData;
              try {
                  //未选择退回原因弹窗提醒
                  tabledata.forEach((item) => {
                      let temp = [];
                      if (item.reject_reason == "") {
                          err = "请检查勾选的条码号是否已经选择退回原因,请选择后再保存！";
                          throw new Error("请检查勾选的条码号是否已经选择退回原因,请选择后再保存！");
                      } else {
                          temp.push(item.barcode);
                          temp.push(item.reject_reason);
                          temp.push(this.rejhandflag); //item.deletereport
                          temp.push(item.description);
                          temp.push(item.tellman);
                          temp.push(item.rejectmsg); //退回信息
                          temp.push(this.loginuserno);
                          temp.push(item.reqrejectimage);
                          if (item.havegroupid == 1) temp.push(item.req_groupid); //如果没有开窗选分单类别，就不传分单类别到后台
                          rejectinfo.push(temp);
                      }
                  });
              } catch (e) {
                  if (e.message != "End") throw e;
              }
              if (err != "") {
                  this.$alert(err, "提示");
                  return false;
              }
              this.$tool
                  .request({
                      url: "REQ/REQ1/REQ115",
                      method: "post",
                      data: {
                          action: "rejectbarcodes",
                          rejectinfo: rejectinfo,
                      },
                  })
                  .then((res) => {
                      this.msglist = []; //获取失败信息列表
                      let faildcnt = 0;
                      if (res.faildBarcode.length > 0) {
                          res.faildBarcode.forEach((item) => {
                              let msg = item.Item1 + "," + item.Item2;
                              this.msglist.push(msg);
                              faildcnt++;
                          });
                      }
                      if (faildcnt > 0) {
                          this.$alert(this.msglist.join(","), "失败提示");
                      } else {
                          this.$pub.message(this, "数据已经退回并取消采样！", "success");
                          this.dialogBackinfo = false;
                          this.FindData(0);
                      }
                  })
                  .catch((e) => {
                      this.$alert(e, "提示");
                  });
          }
      },
      cancelall(record) {
          for (let i = 0, len = record.length; i < len; i++) {
              this.$tool
                  .request({
                      url: "REQ/REQ1/REQ115",
                      method: "post",
                      data: {
                          action: "cancelall",
                          barcode: record[i].barcode,
                      },
                  })
                  .then((res) => {
                      if (res.flag == 1) {
                          record[i].req_stat = "9";
                          record[i].req_statname = "作废/取消";
                          this.$pub.message(this, "已取消采样", "success");
                      } else {
                          this.$message.error("取消采样失败:" + res.msg);
                      }
                  });
          }
      },
      checkMethod({ row }) {
          if (row.chkflag == "1") {
              //退回条码可勾选
              return true;
          } else {
              return false;
          }
      },
      CellActive({ row, columnIndex }) {
          if (row.chkflag == "1") {
              //退回条码可勾选
              return true;
          } else {
              return false;
          }
      },
  },
};
</script>
<style scoped>
/deep/.vxe-input--wrapper .vxe-input {
  height: auto;
  width: 200px;
  border-radius: 0px;
}
/deep/.el-button--default.btn-custom-cancel {
  float: right !important;
  margin-left: 10px;
}

.pagetop {
  left: 0;
  right: 0;
  height: 113px;
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 118px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 100%;
  right: 305px;
  left: 0;
}

.middle-right {
  position: absolute;
  top: 0;
  bottom: 2px;
  width: 300px;
  right: 0px;
}
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
}
/deep/.el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
/deep/.el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
/deep/ .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
/deep/.el-range-editor.el-input__inner {
  width: 200px;
}
/deep/.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 130px;
}
</style>
